package com.zhou.system.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.common.lib.exception.BizException;
import com.zhou.common.lib.util.BeanUtil;
import com.zhou.system.management.entity.SysDeptUser;
import com.zhou.system.management.entity.SysPost;
import com.zhou.system.management.mapper.SysPostMapper;
import com.zhou.system.management.model.ao.SysDeptAddAo;
import com.zhou.system.management.model.ao.SysPostUpdateAo;
import com.zhou.system.management.service.ISysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.zhou.system.management.enums.ManageErrorCodeEnum.POST_EXSITS_USERS__ERROR;
import static com.zhou.system.management.enums.ManageErrorCodeEnum.POST_NOT_EXISTS_ERROR;

/**
 * <p>
 * 岗位信息表 服务实现类
 * </p>
 *
 * @author zxf
 * @since 2023-05-18
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements ISysPostService {

    @Autowired
    private SysDeptUserServiceImpl deptUserService;

    @Override
    public void add(SysDeptAddAo addVo) {
        SysPost sysPost = BeanUtil.copyObject(addVo, SysPost.class);
        save(sysPost);
    }

    @Override
    public void update(SysPostUpdateAo updateVo) {
        SysPost sysPost = BeanUtil.copyObject(updateVo, SysPost.class);
        updateById(sysPost);
    }

    @Override
    public void delete(Long postId) {
        SysPost post = getById(postId);
        if (post == null) {
            throw new BizException(POST_NOT_EXISTS_ERROR);
        }
        LambdaQueryWrapper<SysDeptUser> queryWrapper = Wrappers.<SysDeptUser>lambdaQuery()
                .eq(SysDeptUser::getPostId, postId);
        int count = deptUserService.count(queryWrapper);
        if (count > 0) {
            throw new BizException(POST_EXSITS_USERS__ERROR);
        }
        removeById(postId);
    }
}
